package org.csu.jpetstore_front.controller.front;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.alipay.api.AlipayApiException;
import org.csu.jpetstore_front.common.CommonResponse;
import org.csu.jpetstore_front.entity.*;
import org.csu.jpetstore_front.node.PassToken;
import org.csu.jpetstore_front.service.OrderService;
import org.csu.jpetstore_front.service.alipay.bean.AlipayBean;
import org.csu.jpetstore_front.service.alipay.service.AlipayService;
import org.csu.jpetstore_front.vo.AccountVO;
import org.csu.jpetstore_front.vo.CCartVO;
import org.csu.jpetstore_front.vo.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/orders/")
@CrossOrigin
public class OrderController {
    @Autowired
    private OrderService orderService;

    @Autowired
    private AlipayService alipayService;

    @GetMapping("id/{orderid}")
    @ResponseBody
    @PassToken
    public CommonResponse<List<OrderVO>> getOrder(@PathVariable("orderid") int orderId){
        return orderService.getOrder(orderId);
    }
    @GetMapping("user/{username}")
    @ResponseBody
    @PassToken
    public CommonResponse<List<OrderVO>> getOrderByUsername(@PathVariable("username") String username){
        return orderService.getOrderByUsername(username);
    }


    @GetMapping(value = "pay",produces = {"text/html;charset=UTF-8"})
    @ResponseBody
    @PassToken
    public CommonResponse<String> pay(@RequestParam("outTradeNo") String outTradeNo,
                                         @RequestParam("subject") String subject,
                                         @RequestParam("totalAmount") String totalAmount,
                                         @RequestParam("body") String body
    ) throws AlipayApiException {

        AlipayBean alipayBean = new AlipayBean();
        //订单号(必填)
        alipayBean.setOut_trade_no(outTradeNo);
        //订单名称(必填)
        alipayBean.setSubject(subject);
        //支付金额(必填)
        alipayBean.setTotal_amount(totalAmount);
        //商品描述(可空)
        alipayBean.setBody(body);
        //支付结果
        String pay = alipayService.aliPay(alipayBean);
//        model.addAttribute("form", pay);
        System.out.println(pay);
        return CommonResponse.createForSuccess(pay);
    }

    @RequestMapping("/paySuccess")
    public CommonResponse<String> paySuccess(HttpSession session) {

//        int order_id = ((Order)session.getAttribute("order")).getOrderId();
//        int order_id = 1;
        return null;
    }

    @PutMapping("insert")
    @ResponseBody
    @PassToken
    public CommonResponse<OrderVO> insertIntoCartShop(OrderVO orderVO){
        return orderService.insertIntoCartShop(orderVO);
    }
    @DeleteMapping("delete")
    @ResponseBody
    @PassToken
    public CommonResponse<OrderVO> deleteFromCartShop(OrderVO orderVO){
        return orderService.deleteItemFromCartShop(orderVO);
    }
    @PatchMapping("update")
    @ResponseBody
    @PassToken
    public CommonResponse<OrderVO> updateQTY(OrderVO orderVO){
        return orderService.updateItemQuantity(orderVO);
    }
    @DeleteMapping("deleteall")
    @ResponseBody
    @PassToken
    public  CommonResponse<AccountVO> deleteAll(AccountVO accountVO){
        return  orderService.deleteAll(accountVO);
    }
    @PutMapping("toorder")
    @ResponseBody
    @PassToken
    public CommonResponse<Integer> insertOrder(Order order,HttpSession session){
        List<OrderVO> orderVOList=(List<OrderVO>) session.getAttribute("items");
        return orderService.insertOrder(order,orderVOList);
    }
    @PutMapping("Orderid")
    @ResponseBody
    @PassToken
    public CommonResponse<List<CCartVO>> insertCartshop(@RequestBody  List<CCartVO> orderList , HttpSession session){
        session.setAttribute("items",orderList);
        System.out.println(orderList);
        return orderService.deletecartshop(orderList);
    }
}